package com.goktech.olala.client.controller.index;

import com.goktech.olala.core.req.ReqGoodKey;
import com.goktech.olala.core.resp.RespArticleVo;
import com.goktech.olala.core.resp.RespCollectVo;
import com.goktech.olala.core.service.IArticleService;
import com.goktech.olala.core.service.ICollectService;
import com.goktech.olala.core.service.IGoodsService;
import com.goktech.olala.core.service.IShopcartService;
import com.goktech.olala.server.pojo.goods.Collect;
import com.goktech.olala.server.pojo.goods.Good;
import com.goktech.olala.server.pojo.order.Shopcart;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname IndexGoodsController
 * @Description TODO
 * @Date 2020/12/5 20:24 11111111111111111111111111111111111111
 * @Created by sanming
 */
@RestController
@RequestMapping("/goodsIndexApi")
public class GoodsIndexController {
    @Autowired
    IGoodsService iGoodsService;

    @Autowired
    IShopcartService iShopcartService;

    @Autowired
    ICollectService iCollectService;

    @Autowired
    IArticleService iArticleService;

    /**
     * 商品搜索
     * @param request
     * @return
     */
    @RequestMapping("/queryGoodsByParam.do")
    public ModelAndView queryGoodsByParam(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();

        String keywords = request.getParameter("keywords");

        ReqGoodKey goodKey=new ReqGoodKey();

        goodKey.setKeywords(keywords);

        List<Good> goods = iGoodsService.selectByKey(goodKey);

        mav.addObject("goodsList",goods);
        mav.setViewName("home/search");
        return mav;
    }


    @RequestMapping(value = "/queryGoodsDetail.do")
    @ResponseBody
    public ModelAndView queryGoodsDetail(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView();
        String articleId = request.getParameter("articleId");
        String goodsId = request.getParameter("goodsId");
        String businessId = request.getParameter("businessId");

        RespCollectVo collectVo = new RespCollectVo();
        if(StringUtils.isNotBlank(articleId)){
            RespArticleVo respVo = iArticleService.queryArticleById(Integer.parseInt(articleId));
            view.addObject("articleInfo", respVo);
        }

        view.addObject("collectVo", collectVo);
        view.setViewName("home/introduction");
        return view;
    }

    /**
     * 添加收藏
     * @return
     */
    @RequestMapping("/saveCollect.do")
    @ResponseBody
    public String saveCollect(HttpServletRequest request){
        String shopCartId = request.getParameter("shopCartId");

        String customerId = (String)request.getSession().getAttribute("customerId");
        if (customerId==null){
            return "error";
        }
        Shopcart shopcart = iShopcartService.selectCollect(new Long(shopCartId), customerId);
        String goodsId = shopcart.getGoodsId();

        Collect collect = new Collect();
        collect.setCustomerId(customerId);
        collect.setGoodsId(goodsId);
        iCollectService.insert(collect);
        return "success";
    }



}
